package com.bailan.foreat.service.impl;

import com.bailan.foreat.entity.Item;
import com.bailan.foreat.entity.Order;
import com.bailan.foreat.entity.Product;
import com.bailan.foreat.mapper.ItemMapper;
import com.bailan.foreat.mapper.OrderMapper;
import com.bailan.foreat.mapper.ProductMapper;
import com.bailan.foreat.mapper.StoreMapper;
import com.bailan.foreat.service.ItemService;
import com.bailan.foreat.vo.OrderIdItemsVo;
import com.bailan.foreat.vo.OrderItemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    OrderMapper orderMapper;


    @Override
    public Page<OrderIdItemsVo> selectMyItem(Integer currentPage, Integer pageSize, Integer storeId) {
//        Page<OrderItemVo> page = new Page<>(currentPage,pageSize);

        List<OrderItemVo> orderItemVos = new ArrayList<>();
        //查出所有商品列表
        QueryWrapper<Product> queryWrapperProduct = new QueryWrapper<>();
        queryWrapperProduct.eq("store_id", storeId);
        //查出状态为退款中的订单列表
        QueryWrapper<Item> queryWrapperItem = new QueryWrapper<>();
        queryWrapperItem.eq("status", 3);
        List<Product> products = productMapper.selectList(queryWrapperProduct);
        //遍历所有product，根据product_id找出所有退款列表
        for (Product product : products) {
            queryWrapperItem.eq("status", 3);
           // QueryWrapper<Product> productQueryWrapper = queryWrapperProduct.eq("product_id", item.getProductId());
            QueryWrapper<Item> itemQueryWrapper = queryWrapperItem.eq("product_id", product.getId());
            List<Item> items = itemMapper.selectList(itemQueryWrapper);
//            System.out.println("items:"+items);

            if (items.isEmpty()){
                continue;
            }else{
                for (Item item : items) {
                    OrderItemVo orderItemVo=new OrderItemVo();
                    orderItemVo.setProduct(product);
                    orderItemVo.setItem(item);
                    //即 当前商户所有的退款列表
                    orderItemVos.add(orderItemVo);
                }
            }
            itemQueryWrapper.clear();
        }
        List<OrderIdItemsVo> orderIdItemsVos = new ArrayList<>();

        //查询所有订单
        QueryWrapper<Order> queryWrapperOrder = new QueryWrapper<>();

        //放置所有当前商户的订单，不重复
        List<Order> newOrders = new ArrayList<>();

        //遍历所有的退款订单列表
        for (OrderItemVo orderItemVo : orderItemVos) {
//            System.out.println("orderItemVo:"+orderItemVo);
            //取出退款列表对应的订单id
            Integer orderId = orderItemVo.getItem().getOrderId();
//            System.out.println("orderId:"+orderId);
            //查询订单id对应的订单
            QueryWrapper<Order> orderQueryWrapper = queryWrapperOrder.eq("id", orderId);
            Order order = orderMapper.selectOne(orderQueryWrapper);
//            System.out.println("order:"+order);
            //如果order订单有重复的话，放到items里面
            if (newOrders.contains(order)){
//                System.out.println("newOrders1:"+newOrders);
//                System.out.println("newOrders.get(0):"+newOrders.get(0));
                for (OrderIdItemsVo orderIdItemsVo : orderIdItemsVos) {
                    if (orderIdItemsVo.getOrder().equals(order)){
                        List<OrderItemVo> orderItemVos1 = orderIdItemsVo.getOrderItemVos();
                        orderItemVos1.add(orderItemVo);
                        orderIdItemsVo.setOrderItemVos(orderItemVos1);
                        System.out.println("orderIdItemsVo:"+orderIdItemsVo);
                    }
                }
            }else{
                //将order对象放进newOrders容器里
                newOrders.add(order);
//                System.out.println("newOrders2:"+newOrders);
//                System.out.println("newOrders.get(0):"+newOrders.get(0));
                //order+orderItemVos的对象
                OrderIdItemsVo orderIdItemsVo = new OrderIdItemsVo();
                orderIdItemsVo.setOrder(order);
                ArrayList<OrderItemVo> neworderItemVos = new ArrayList<>();
                neworderItemVos.add(orderItemVo);
                orderIdItemsVo.setOrderItemVos(neworderItemVos);
                orderIdItemsVos.add(orderIdItemsVo);
            }
            queryWrapperOrder.clear();
        }
        Page<OrderIdItemsVo> orderIdItemsVoPage = new Page<>(currentPage,pageSize);

        System.out.println("...orderIdItemsVos...:"+orderIdItemsVos);
        List<OrderIdItemsVo> orderIdItemsVos1;
        if (orderIdItemsVos.size()%pageSize!=0&&currentPage*pageSize>orderIdItemsVos.size()){
            System.out.println("...(currentPage-1)*pageSize...:"+(currentPage-1)*pageSize);
            System.out.println("...orderIdItemsVos.size()...:"+orderIdItemsVos.size());
            orderIdItemsVos1=orderIdItemsVos.subList((currentPage-1)*pageSize,orderIdItemsVos.size());
        }else{
            System.out.println("...(currentPage-1)*pageSize...:"+(currentPage-1)*pageSize);
            System.out.println("...currentPage*pageSize...:"+currentPage*pageSize);
            orderIdItemsVos1 = orderIdItemsVos.subList((currentPage-1)*pageSize,currentPage*pageSize);
        }
        orderIdItemsVoPage.setRecords(orderIdItemsVos1);
        System.out.println("...orderIdItemsVos1...:"+orderIdItemsVos1);

        orderIdItemsVoPage.setTotal(orderIdItemsVos.size());
        System.out.println("...orderIdItemsVos.size()...:"+orderIdItemsVos.size());
//        orderIdItemsVoPage.setPages(orderIdItemsVos.size()/pageSize+1);
        orderIdItemsVoPage.setSize(pageSize);
        System.out.println("...pageSize...:"+pageSize);
        orderIdItemsVoPage.setCurrent(currentPage);
        System.out.println("...currentPage...:"+currentPage);

        return orderIdItemsVoPage;
    }

    @Override
    public void updateItemStatus(List<OrderItemVo> orderItemVos) {
//        System.out.println("新1orderIdItemsVo:"+orderIdItemsVo);
        for (OrderItemVo orderItemVo : orderItemVos) {
//            System.out.println("新2orderIdItemsVo:"+orderIdItemsVo);
            Item item = orderItemVo.getItem();
            System.out.println("...item...:"+item);
            item.setStatus(4);
            itemMapper.updateById(item);
        }
    }
}
